import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

class UserInfo {
  String nickname;
  int level;

  UserInfo(this.nickname, this.level);
}
//在组件之间如果有事件需要传递，一方面可以一层层来传递，另一方面我们也可以使用一个EventBus工具来完成。
//1.创建全局的EventBus对象
final EventBus eventBus = EventBus();

void main() => runApp(MyApp());

//2.在某个Widget中，发出事件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
          appBar: AppBar(
            title: Text('手势'),
          ),
          body: Center(
            child: Column(
              children: <Widget>[HYButton(), HYText()],
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
            ),
          )),
    );
  }
}

class HYButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text("按钮"),
      onPressed: () {
        final info = UserInfo("shy", 18);
        eventBus.fire(info);
      },
    );
  }
}

class HYText extends StatefulWidget {
  @override
  _HYTextState createState() => _HYTextState();
}

class _HYTextState extends State<HYText> {
  String _message = "hello";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
//    3.在某个Widget中，监听事件
    eventBus.on<UserInfo>().listen((data) {
      setState(() {
        _message = "${data.nickname}-${data.level}";
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Text(
      _message,
      style: TextStyle(fontSize: 20),
    );
  }
}

class solveNestedBugDemo extends StatelessWidget {
  const solveNestedBugDemo({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        GestureDetector(
          onTapDown: (details) {
            print("点击1");
          },
          child: Container(
            width: 200,
            height: 200,
            color: Colors.red,
          ),
        ),
//              忽略点击事件
        GestureDetector(
          onTapDown: (details) {
            print("点击2");
          },
          child: Container(
            width: 100,
            height: 100,
            color: Colors.yellow,
          ),
        )
      ],
    );
  }
}

// 嵌套点击会传入父视图 系统bug 点击子视图有可能导致父视图也响应点击事件 可以通过Stack方式解决 就是取消两者的嵌套关系
class nestedBugDemo extends StatelessWidget {
  const nestedBugDemo({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (details) {
        print("点击1");
      },
      child: Container(
        width: 200,
        height: 200,
        color: Colors.yellow,
        alignment: Alignment.center,
        child: GestureDetector(
          onTapDown: (details) {
            print("点击2");
          },
          child: Container(
            width: 100,
            height: 100,
            color: Colors.red,
          ),
        ),
      ),
    );
  }
}

class GestureDemo extends StatelessWidget {
  const GestureDemo({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        width: 200,
        height: 200,
        color: Colors.yellow,
      ),
      onDoubleTap: () {
        print("点击两次");
      },
      onTapCancel: () {
        print("点击取消");
      },
      onLongPress: () {
        print("长按");
      },
      onLongPressMoveUpdate: (LongPressMoveUpdateDetails details) {
        print("move23 ${details.localPosition}");
      },
    );
  }
}
